package frame;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;

import MyBatis.MyBatislianjian;
import constant.Constant;
import frame.Customer.CustomerPanel;
import frame.Employee.EmployeePanel;
import frame.Good.GoodPanel;
import frame.PayDetail.PayDetailPanel;
import frame.PayMain.PayMainPanel;
import org.apache.ibatis.session.SqlSession;
import org.example.mapper.EmployeeMapper;
import org.example.pojo.Employee;
import redis.clients.jedis.Jedis;

public class GuanliPanel extends JPanel {
    private static JDesktopPane jDesktopPane = new JDesktopPane();
    private BufferedImage backgroundImage;

    private SqlSession sqlSession = MyBatislianjian.getSqlSession();
    private EmployeeMapper employeeMapper = sqlSession.getMapper(EmployeeMapper.class);

    // Redis 连接信息
    private static final String REDIS_HOST = "localhost";
    private static final int REDIS_PORT = 6379;
    public static final Jedis jedis = new Jedis(REDIS_HOST, REDIS_PORT);

    public GuanliPanel() {
        this.setBounds(0, 0, Constant.width, Constant.height);
        this.setLayout(new BorderLayout());

        // 加载背景图片
        try {
            InputStream imageStream = getClass().getClassLoader().getResourceAsStream("images/chaoshi1.jpg");
            if (imageStream != null) {
                backgroundImage = ImageIO.read(imageStream);
            } else {
                System.err.println("未找到图片: images/chaoshi1.jpg");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 创建菜单栏
        JMenuBar menuBar = new JMenuBar();
        this.add(menuBar, BorderLayout.NORTH);

        // 添加 JDesktopPane 到中心
        ((JComponent) jDesktopPane).setOpaque(false); // 设置 JDesktopPane 透明
        this.add(jDesktopPane, BorderLayout.CENTER);

        // 创建顶级菜单
        JMenu manageMenu = new JMenu("基本信息管理");
        JMenu settingsMenu = new JMenu("采购信息管理");
        JMenu helpMenu = new JMenu("设置");

        // 添加顶级菜单到菜单栏
        menuBar.add(manageMenu);
        menuBar.add(settingsMenu);
        menuBar.add(helpMenu);

        // 添加管理菜单项
        JMenuItem manageEmployees = new JMenuItem("客户管理");
        JMenuItem manageGoods = new JMenuItem("商品管理");
        JMenuItem manageOrders = new JMenuItem("员工管理");

        manageMenu.add(manageEmployees);
        manageMenu.add(manageGoods);
        manageMenu.add(manageOrders);
        manageEmployees.addActionListener(e -> setContent(new CustomerPanel()));
        manageGoods.addActionListener(e -> setContent(new GoodPanel()));
        manageOrders.addActionListener(e -> setContent(new EmployeePanel()));

        // 添加采购管理菜单项
        JMenuItem purchaseGoods = new JMenuItem("采购主表");
        JMenuItem purchaseDetails = new JMenuItem("采购明细");

        settingsMenu.add(purchaseGoods);
        settingsMenu.add(purchaseDetails);

        purchaseGoods.addActionListener(e -> setContent(new PayMainPanel()));
        purchaseDetails.addActionListener(e -> setContent(new PayDetailPanel()));

        // 添加设置菜单项
        JMenuItem jMenuItem = new JMenuItem("个人信息");
        JMenuItem logout = new JMenuItem("注销账号");
        JMenuItem exit = new JMenuItem("退出登录");

        helpMenu.add(jMenuItem);
        helpMenu.add(logout);
        helpMenu.add(exit);

        // 点击个人信息菜单项的事件监听器
        jMenuItem.addActionListener(e -> {
            String eid = LandPanel.getyonghu1();
            //从数据库中获取并缓存到 Redis
            Employee employee = employeeMapper.selectEmployeeByEid(eid);
                    // 将员工信息存入 Redis
            jedis.hset("employees", employee.getEid(), employee.toString());
         setContent(new gerenxinxi());
        });

        // 点击注销账号的事件监听器
        logout.addActionListener(e -> {
            int res = JOptionPane.showConfirmDialog(this,
                    "你确定要注销账号",
                    "提示", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
            if (res == JOptionPane.YES_OPTION) {
                String eid = LandPanel.getyonghu1();
                employeeMapper.deleteEmployee(new String[]{eid});
                jedis.hdel("employees", eid); // 同步删除 Redis 中的缓存
                AppFrame.setContent(new LandPanel());
            }
        });

        // 添加退出登录菜单项的事件监听器
        exit.addActionListener(e -> AppFrame.setContent(new LandPanel()));

        // 监听父容器大小变化事件
        this.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e) {
                adjustDesktopPaneSize();
            }
        });

        adjustDesktopPaneSize(); // 初始调整桌面面板大小
    }

    // 设置内容面板的方法
    public static void setContent(JInternalFrame internalFrame) {
        internalFrame.setVisible(true);
        jDesktopPane.removeAll();
        jDesktopPane.add(internalFrame);
        internalFrame.setSize(jDesktopPane.getWidth(), jDesktopPane.getHeight());
        internalFrame.revalidate(); // 重新布局内部组件
        internalFrame.repaint(); // 刷新界面
    }

    // 调整桌面面板大小的方法
    private void adjustDesktopPaneSize() {
        jDesktopPane.setSize(getWidth(), getHeight());
        Component[] components = jDesktopPane.getComponents();
        for (Component component : components) {
            if (component instanceof JInternalFrame) {
                JInternalFrame internalFrame = (JInternalFrame) component;
                internalFrame.setSize(jDesktopPane.getWidth(), jDesktopPane.getHeight());
                internalFrame.revalidate();
                internalFrame.repaint();
            }
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (backgroundImage != null) {
            g.drawImage(backgroundImage, 0, 0, getWidth(), getHeight(), this);
        } else {
            g.setColor(Color.LIGHT_GRAY);
            g.fillRect(0, 0, getWidth(), getHeight());
        }
    }
}
